package UdpEcho;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.sql.PreparedStatement;

public class UdpEchoServer {
    private DatagramSocket datagramSocket=null;
    public UdpEchoServer(int port) throws SocketException {
        datagramSocket=new DatagramSocket(port);
    }
    public void start() throws IOException {
        System.out.println("服务器启动！");
        while(true){
            DatagramPacket requestPacket=new DatagramPacket(new byte[4048],4048);
            datagramSocket.receive(requestPacket);
            String request=new String(requestPacket.getData(),0,requestPacket.getLength());
            String response=procsee(request);
            DatagramPacket responsePacket=new DatagramPacket(response.getBytes(),response.getBytes().length,
                    requestPacket.getSocketAddress());
            datagramSocket.send(responsePacket);
            System.out.printf("[%s:%d]   req:%s    rep:%s/n",
                    requestPacket.getAddress(),requestPacket.getPort(),request,response);
        }
    }
    public String procsee(String s){
        return s;
    }

    public static void main(String[] args) throws IOException {
        UdpEchoServer server=new UdpEchoServer(8776);
        server.start();
    }
}
